package com.example.yan_music.utils;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * <p>
 *     控制软键盘显示和隐藏的工具类
 * </p>
 */
public class KeyboardUtils {
    /**
     * 显示软键盘
     * @param editText
     */
    public static void showKeyboard(EditText editText) {
        InputMethodManager inputMethodManager = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        // 判空防止空指针
        if (inputMethodManager != null) {
            editText.setFocusable(true);
            editText.setFocusableInTouchMode(true);
            editText.requestFocus();
            inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 隐藏软键盘
     * @param editText
     */
    public static void hideKeyboard(EditText editText){
        InputMethodManager imm = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        // 判空防止空指针
        if (imm != null) {
            editText.clearFocus();
            imm.hideSoftInputFromWindow(editText.getWindowToken(),0);
        }
    }
}
